<?php

namespace Home\Controller;

use Home\Biz\DealerBiz;
use Think\Controller;
use Home\Biz\StringBiz;
use Home\Biz\OrderBiz;
use Home\Biz\MailBiz;
use Home\Biz\SaleBiz;
use Home\Biz\CommonBiz;


class VRDealerController extends LoginBaseController {
	public function partofexpressdetail() {
		$order_id = I ( 'order_id' );
		$oid = I ( 'oid' );
		$order_info_en = M ( 'order_info' )->where ( "order_id='%s'", $order_id )->find ();
		
		if (empty ( $order_info_en )) {
			$error_msg = "订单不存在!";
			redirect ( C ( 'AdminConfig.error_info_page' ) . '?type=202&is_show_button=2&msg=,' . $error_msg );
			return;
		}
		
		if (empty ( $order_info_en ['ship_qty'] )) {
			
			$order_info_en ['ship_qty'] = 0;
		}
		
		if (empty ( $oid )) {
			
			$order_detail_list = M ( 'order_detail' )->where ( "order_id='%s'", $order_id )->select ();
			$this->assign ( "isPart", 1 );
		} else {
			// 部分发货逻辑
			$order_detail_list = M ( 'order_detail' )->where ( "order_id='%s' and oid='%s'", $order_id, $oid )->select ();
			$this->assign ( "isPart", 2 );
		}
		
		foreach ( $order_detail_list as &$order_detail ) {
			
			if (empty ( $order_detail ['ship_qty'] )) {
				
				$order_detail ['ship_qty'] = 0;
			}
		}
		
		$this->assign ( 'order_info_en', $order_info_en );
		$this->assign ( 'order_detail_list', $order_detail_list );
		$this->display ( 'vrdealer/partofexpress' );
	}
	public function partofexpress() {
		$order_id = I ( 'order_id' );
		$isPart = I ( 'isPart' );
		$logistic_type = I ( 'logistic_type' );
		$logistic_name = I ( 'logistic_name' );
		$express_no = I ( 'express_no' );
		$shipping_warehouse =I('shipping_warehouse');
		$remark = I ( 'remark' );
		$order_info_en = M ( 'order_info' )->where ( "order_id='%s'", $order_id )->find ();
		
		if (empty ( $order_info_en )) {
			$error_msg = "订单不存在!";
			redirect ( C ( 'AdminConfig.error_info_page' ) . '?type=202&is_show_button=2&msg=,' . $error_msg );
			return;
		}
		
		
		//余额支付扣钱逻辑
		//支付类型为余额支付且支付状态为未支付的VR渠道经销商订单
		if($order_info_en['payment_type'] ==3 &&$order_info_en['payment_status']==0 && $order_info_en ['order_status'] == 10 && $order_info_en ['order_type'] == 10){
			
			//查看该VR渠道经销商账户余额
			
			$dealer_buyer_en= M('dealer_info')->where("dealer_id='%s'",$order_info_en['buyer_alipay_no'])->find();
			
			if($dealer_buyer_en['amount']-$order_info_en['payment_amount'] <0){
				
				$error_msg = "VR渠道经销商余额不足,请先充值!";
				redirect ( C ( 'AdminConfig.info_page' ) . '?type=202&is_show_button=2&msg=' . $error_msg );
				return;
			}
			
			//扣款并更改相关状态
			//扣VR渠道经销商余额
			
			M('dealer_info')->where("dealer_id='%s'",$order_info_en['buyer_alipay_no'])->setField('amount',$dealer_buyer_en['amount']-$order_info_en['payment_amount']);
			//插入VR渠道经销商余额表
			$dealer_amount = array(
					"dealer_amount_id"	=> DealerBiz::generateDealerAmountId(),
					"dealer_id" =>$dealer_buyer_en['dealer_id'],
					"dealer_name" =>$dealer_buyer_en['dealer_name'],
					"amount_kind" =>2,//减少
					"amount_type" =>24,//订单支付
					"amount" =>$order_info_en['payment_amount'],//订单金额
					"begin_amount" =>$dealer_buyer_en['amount'],
					"end_amount" =>$dealer_buyer_en['amount']-$order_info_en['payment_amount'],
					"order_id" => $order_info_en['order_id'],
					"remark" =>"订单支付",
					"status" =>1,//有效
					'create_user' => "whaleyinfo.order DealerBiz.partofexpress",
					'create_date' => date ( "y-m-d H:i:s", time () )
			);
			
			M('dealer_amount')->add($dealer_amount);
			
			//更新订单状态为已支付并插入日志
			$order_status_update =array(
				'payment_method'	=>30,//余额支付
				'payment_status' =>1,//已支付
				'order_status' =>22 ,//已支付					
			);
			
			M('order_info')->where("order_id='%s'",$order_info_en['order_id'])->setField($order_status_update);
			$order_change_log = array (
					"order_id" => $order_id,
					"order_status" => 22,//支付
					"order_status_text" => StringBiz::getPromoterOrderStatusText(22),
					"create_user" => 'whaleyinfo.order dealer.partofexpress',
					"create_date" => date ( "y-m-d H:i:s", time () )
			);
			M ( 'order_status_change_log' )->data ( $order_change_log )->add ();
			//更新订单支付表支付状态
			M('order_payment')->where("order_id='%s'",$order_info_en['order_id'])->setField("payment_status",1);
		}

		
		//必须重新查询订单状态
		$order_info_en = M ( 'order_info' )->where ( "order_id='%s'", $order_id )->find ();
		
		if ($isPart == 1) {
			
			// 1.检查VR渠道经销商订单状态是否为已支付状态或者部分发货
			
			if (($order_info_en ['order_status'] == 33 || $order_info_en ['order_status'] == 22) && $order_info_en ['order_type'] == 10) {
				
				$order_detail_list = M ( 'order_detail' )->where ( "order_id='%s'", $order_id )->select ();
				
				// 更改订单，订单明细状态已及发货的数量
				// 实发数量等于商品总数
				$order_info_en ['ship_qty'] = $order_info_en ['goods_qty'];
				// 订单状态为全部发货
				$order_info_en ['order_status'] = 31;
				
				foreach ( $order_detail_list as &$order_detail ) {
					// 更新订单明细实发数量生成插入物流信息并写入数据库
					if (empty ( $order_detail ['ship_qty'] )) {
						
						$order_detail ['ship_qty'] = 0;
					}
					
					$order_consign = array (
							
							'order_logistic_id' => CommonBiz::getExpressId ( "Express" ),
							'order_id' => $order_info_en ['order_id'],
							'oid' => $order_detail ['oid'],
							// 实发数量是总数减去已发货的数量
							'ship_qty' => $order_detail ['qty'] - $order_detail ['ship_qty'],
							'logistic_type' => $logistic_type,
							'logistic_name' => $logistic_name,
							'shipping_warehouse' =>$shipping_warehouse,
							'express_no' => $express_no,
							'remark' => $remark,
							'create_user' => "whaleyinfo.order DealerBiz.partofexpress",
							'create_date' => date ( "y-m-d H:i:s", time () ) 
					);
					
					$order_detail ['ship_qty'] = $order_detail ['qty'];
					
					M ( 'order_consignment' )->add ( $order_consign );
					M ( 'order_detail' )->where ( "order_id='%s' and oid='%s'", $order_id, $order_detail ['oid'] )->save ( $order_detail );
				}
				
				// 写订单数据库
				M ( 'order_info' )->where ( "order_id='%s'", $order_id )->save ( $order_info_en );
				// 写订单日志表
				$order_change_log = array (
						"order_id" => $order_id,
						"order_status" => 31,
						"order_status_text" => '全部发货',
						"create_user" => 'whaleyinfo.order dealer.partofexpress',
						"create_date" => date ( "y-m-d H:i:s", time () ) 
				);
				M ( 'order_status_change_log' )->data ( $order_change_log )->add ();
				
				$succ_msg = "订单全部发货操作成功!";
				redirect ( C ( 'AdminConfig.info_page' ) . '?type=202&is_show_button=2&msg=成功,' . $succ_msg );
				return;
			} else {
				
				$error_msg = "订单状态非法,全部发货操作仅限于已支付的VR渠道经销商订单!";
				redirect ( C ( 'AdminConfig.error_info_page' ) . '?type=202&is_show_button=2&msg=,' . $error_msg );
				return;
			}
		} elseif ($isPart == 2) {
			
			$ship_qty = I ( 'ship_qty' );
			$order_detail_id = I ( 'order_detail_id' );
			
			// 检查VR渠道经销商订单为部分发货或者已支付状态
			if (($order_info_en ['order_status'] == 33 || $order_info_en ['order_status'] == 22) && $order_info_en ['order_type'] == 10) {
				
				$order_detail_audit = M ( 'order_detail' )->where ( "order_id='%s' and oid='%s'", $order_id, $order_detail_id )->find ();
				$order_detail_list = M ( 'order_detail' )->where ( "order_id='%s'", $order_id )->select ();
				// 判断该订单明细是否存在
				if (empty ( $order_detail_audit )) {
					
					$error_msg = "该订单明细不存在!";
					redirect ( C ( 'AdminConfig.error_info_page' ) . '?type=202&is_show_button=2&msg=,' . $error_msg );
					return;
				}
				
				// 判断该订单明细提交的发货数量是否合法
				
				if (empty ( $order_detail_audit ['ship_qty'] )) {
					
					$order_detail_audit ['ship_qty'] = 0;
				}
				
				if (empty ( $order_info_en ['ship_qty'] )) {
					
					$order_info_en ['ship_qty'] = 0;
				}
				
				if (($order_detail_audit ['qty'] - $order_detail_audit ['ship_qty']) <= 0) {
					
					$error_msg = "该订单明细没有剩余待发的货物数量!";
					redirect ( C ( 'AdminConfig.error_info_page' ) . '?type=202&is_show_button=2&msg=,' . $error_msg );
					return;
				} elseif ($ship_qty > ($order_detail_audit ['qty'] - $order_detail_audit ['ship_qty'])) {
					
					$error_msg = "发货的数量大于该订单明细剩余待发的货物数量!";
					redirect ( C ( 'AdminConfig.error_info_page' ) . '?type=202&is_show_button=2&msg=,' . $error_msg );
					return;
				} else {
					
					// 该订单明细正常走正常发货流程
					// 更新该订单明细实发的数量，累加
					
					$order_detail_audit ['ship_qty'] = $order_detail_audit ['ship_qty'] + $ship_qty;
					
					// 根据订单明细更改订单状态已及发货的数量
					// 计算已发的总的数量,已累计本次提交的数据
					$total_shipqty = 0;
					foreach ( $order_detail_list as &$order_detail ) {
						
						if (empty ( $order_detail ['ship_qty'] )) {
							
							$order_detail ['ship_qty'] = 0;
						}
						
						if ($order_detail ['oid'] == $order_detail_id) {
							
							$total_shipqty = $order_detail_audit ['ship_qty'] + $total_shipqty;
						} else {
							
							$total_shipqty = $order_detail ['ship_qty'] + $total_shipqty;
						}
					}
					
					// 判断订单状态
					if ($total_shipqty > $order_info_en ['goods_qty']) {
						$error_msg = "发货的数量大于该订单剩余待发的货物数量!";
						redirect ( C ( 'AdminConfig.error_info_page' ) . '?type=202&is_show_button=2&msg=,' . $error_msg );
						return;
					} elseif ($total_shipqty == $order_info_en ['goods_qty']) {
						// 全部发货
						$order_info_en ['ship_qty'] = $total_shipqty;
						// 订单状态为全部发货
						$order_info_en ['order_status'] = 31;
					} else {
						// 部分发货
						$order_info_en ['ship_qty'] = $total_shipqty;
						// 订单状态为全部发货
						$order_info_en ['order_status'] = 33;
					}
					
					$order_consign = array (
							
							'order_logistic_id' => CommonBiz::getExpressId ( "Express" ),
							'order_id' => $order_info_en ['order_id'],
							'oid' => $order_detail_audit ['oid'],
							'ship_qty' => $ship_qty,
							'logistic_type' => $logistic_type,
							'logistic_name' => $logistic_name,
							'shipping_warehouse' =>$shipping_warehouse,
							'express_no' => $express_no,
							'remark' => $remark,
							'create_user' => "whaleyinfo.order DealerBiz.partofexpress",
							'create_date' => date ( "y-m-d H:i:s", time () ) 
					);
					
					$order_change_log = array (
							"order_id" => $order_id,
							"order_status" => $order_info_en ['order_status'],
							"order_status_text" => StringBiz::getPromoterOrderStatusText ( $order_info_en ['order_status'] ),
							"create_user" => 'whaleyinfo.order dealer.partofexpress',
							"create_date" => date ( "y-m-d H:i:s", time () ) 
					);
					M ( 'order_info' )->where ( "order_id='%s'", $order_id )->save ( $order_info_en );
					M ( 'order_detail' )->where ( "order_id='%s' and oid='%s'", $order_id, $order_detail_audit ['oid'] )->save ( $order_detail_audit );
					M ( 'order_consignment' )->add ( $order_consign );
					M ( 'order_status_change_log' )->data ( $order_change_log )->add ();
					
					$succ_msg = "订单部分发货操作成功!";
					redirect ( C ( 'AdminConfig.info_page' ) . '?type=202&is_show_button=2&msg=成功,' . $succ_msg );
					return;
				}
			} else {
				
				$error_msg = "订单状态非法,部分发货操作仅限于已经处于部分发货或者已支付的VR渠道经销商订单!";
				redirect ( C ( 'AdminConfig.error_info_page' ) . '?type=202&is_show_button=2&msg=,' . $error_msg );
				return;
			}
		} else {
			$error_msg = "操作流程不合规,请检查";
			redirect ( C ( 'AdminConfig.error_info_page' ) . '?type=202&is_show_button=2&msg=,' . $error_msg );
			return;
		}
	}
	public function detail() {
		$order_id = I ( 'order_id' );
		$order_info_en = M ( 'order_info' )->where ( "order_id='%s'", $order_id )->find ();
		$order_discount_en = M ( 'order_discount' )->where ( "order_id='%s' AND discount_type=41", $order_id)->find ();
		$order_payment_en = M ( 'order_payment' )->where ( "order_id='%s'", $order_id )->find ();
		$order_detail_en = M ( 'order_detail' )->where ( "order_id='%s'", $order_id )->select ();
		$order_info_en ['order_status_text'] = StringBiz::getPromoterOrderStatusText ( $order_info_en ['order_status'] );
		$order_info_en ['order_type_text'] = StringBiz::getOrderTypeText ( $order_info_en ['order_type'] );
		$order_info_en ['payment_method_text'] = StringBiz::getPaymentMethodText ( $order_info_en ['payment_method'] );
		
		$order_consign_en = M ( 'order_consignment' )->where ( "order_id='%s'", $order_id )->select ();
		$order_change_log_en = M ( 'order_status_change_log' )->where ( "order_id='%s'", $order_id )->order("create_date desc")->select ();
		if (empty ( $order_info_en ['ship_qty'] )) {
			
			$order_info_en ['ship_qty'] = 0;
		}
		
		foreach ( $order_detail_en as &$order_detail ) {
			
			if (empty ( $order_detail ['ship_qty'] )) {
				
				$order_detail ['ship_qty'] = 0;
			}
		}
		
		$this->assign ( 'order_info_en', $order_info_en );
		$discount_amount=$order_discount_en[discount_amount];
		$this->assign ( 'discount_amount', abs($discount_amount) );
		$this->assign ( 'order_payment_en', $order_payment_en );
		$this->assign ( 'order_detail_en', $order_detail_en );
		$this->assign ( "order_consign_en", $order_consign_en );
		$this->assign("order_change_log_en",$order_change_log_en);
		$this->assign ( 'page_name', '订单明细' );
		$this->display ( 'vrdealer/dealerOrderDetail' );
	}
	public function paymentVoucherUpload() {
		$order_id = I ( 'order_id' );
		$this->assign ( 'order_id', $order_id );
		$this->display ( 'vrdealer/paymentVoucherUpload' );
	}
	public function paymentVoucherUploadAction() {
		$order_id = I ( 'order_id' );
		
		/**
		 * * 图片上传 start **
		 */
		$upload_config = array (
				'maxSize' => 0,
				'rootPath' => './Public/Uploads/',
				'savePath' => 'paymentVoucher/' . $order_id . '/',
				'saveName' => array (
						'uniqid',
						'' 
				),
				'exts' => array (
						'jpg',
						'gif',
						'png',
						'jpeg' 
				),
				'subName' => '',
				'replace' => true 
		);
		
		$upload->autoSub = false;
		$upload = new \Think\Upload ( $upload_config ); // 实例化上传类
		
		$info = $upload->upload ();
		if (! $info) {
			// 转到注册失败页面
			echo $upload->getError ();
			return;
		} else {
			foreach ( $info as $file ) {
				$key = $file ['key'];
				switch ($key) {
					case 'img_payment_voucher' :
						$payment_voucher = $file ['savepath'] . $file ['savename'];
					default :
						break;
				}
			}
		}
		/**
		 * * 图片上传 end **
		 */
		
		$error_msg = M ( 'order_payment' )->where ( "order_id='%s'", $order_id )->setField ( 'payment_voucher', $payment_voucher );
		
		if (! $error_msg) {
			redirect ( C ( 'AdminConfig.error_info_page' ) . '?type=201&is_show_button=2&msg=,更新订单支付凭证失败。' . $error_msg );
			return;
		} else {
			M ( 'order_info' )->where ( "order_id='$order_id'" )->setField ( 'order_status', '23' );
			MailBiz::callMailApi ( C ( 'VREmailConfig.PaymentVoucherUploadReceiverList' ), 'VR渠道经销商渠道订单' . $order_id . C ( 'VREmailConfig.PaymentVoucherUploadMailSubject' ), 'VR渠道经销商渠道订单' . $order_id . C ( 'VREmailConfig.PaymentVoucherUploadMailSubject' ) );
			redirect ( C ( 'AdminConfig.info_page' ) . '?type=201&is_show_button=2&msg=订单支付凭证上传成功。' );
			return;
		}
	}
	public function addDealer() {
		$this->assign ( 'page_name', 'VR渠道经销商信息添加' );
		$this->display ( 'vrdealer/adddealer' );
	}
	
	public function stockOrderImport() {
		$this->assign ( 'page_name', '订单导入' );
		$this->display ( 'vrdealer/stockOrderImport' );
	}
	
	public function stockOrderImportAction(){
	
		$upload_config = array (
				'maxSize' => 0,
				'rootPath' => './Public/Uploads/',
				'savePath' => 'k3_code_order_import/',
				'saveName' => 'order_import',
				'exts' => array ('xls'),
				'subName' => '',
				'replace' => true 
		);
		
		$upload = new \Think\Upload ( $upload_config ); // 实例化上传类
		$upload->autoSub = false;
		
		$info = $upload->upload ();
		if (! $info) {
			// 上传错误提示错误信息
			redirect ( 'dealerOrderUploadResult?msg=上传失败！' . $upload->getError () );
		} else {
			// 上传成功
			// 读取csv文件将相关数据写入数据库
			// $xls_file = fopen('./Public/Uploads/stockOrderImport/stockOrderImport.xls','r');
			$err_msg = DealerBiz::k3_coder_order_import ( './Public/Uploads/k3_code_order_import/order_import.xls' );
			if (!empty($err_msg)) {
				redirect ( 'dealerOrderUploadResult?msg=已成功上传并处理！共有'.$err_msg[total_row]."条记录，成功".$err_msg[success]."条记录，失败".$err_msg[failure]."条记录。" );
			} 
		}
	}
	
	public function updateDealerAction() {
		$dealer_id = I ( 'dealer_id' );
		$sign_date = I ( 'sign_date' );
		$contract_start_date = I ( 'contract_start_date' );
		$contract_end_date = I ( 'contract_end_date' );
		
		if (empty ( $sign_date )) {
			$sign_date = null;
		} else {
			
			$sign_date = date ( "y-m-d H:i:s", strtotime ( $sign_date ) );
		}
		
		if (empty ( $contract_start_date )) {
			$contract_start_date = null;
		} else {
			
			$contract_start_date = date ( "y-m-d H:i:s", strtotime ( $contract_start_date ) );
		}
		
		if (empty ( $contract_end_date )) {
			$contract_end_date = null;
		} else {
			
			$contract_end_date = date ( "y-m-d H:i:s", strtotime ( $contract_end_date ) );
		}
		
		$is_exists_dealer = M ( 'dealer_info' )->where ( "dealer_name='%s' and dealer_channel=2 and dealer_id!='%s'", I ( 'dealer_name' ),I('dealer_id') )->find ();
		if (! empty ( $is_exists_dealer )) {
			$error_msg = "VR渠道经销商已存在!";
			redirect ( C ( 'AdminConfig.error_info_page' ) . '?type=202&msg=,' . $error_msg );
			return;
		}
		
		
		$update_dealer_en = array (
				'dealer_id' => $dealer_id,
				'dealer_name' => I ( 'dealer_name' ),
				'dealer_type' => I ( 'dealer_type' ),
				'k3_code' => I ( 'k3_code' ),
				'user_id' => I ( 'user_id_fk' ),
				'discount_rate' => I ( 'discount_rate' ),
				'business_leader' => I ( 'business_leader' ),
				'business_leader_cellphone' => I ( 'business_leader_cellphone' ),
				'area_name' => I ( 'area_name' ),
				'province_id' => I ( 'province_id' ),
				'province_name' => I ( 'province_name' ),
				'city_id' => I ( 'city_id' ),
				'city_name' => I ( 'city_name' ),
				'operation_pattern' => I ( 'operation_pattern' ),
				'registered_capital' => I ( 'registered_capital' ),
				'leal_person' => I ( 'leal_person' ),
				'bank_name' => I ( 'bank_name' ),
				'bank_no' => I ( 'bank_no' ),
				'tax_no' => I ( 'tax_no' ),
				'dealer_id_card_no' => I ( 'dealer_id_card_no' ),
				'dealer_id_card_image_list' => I ( 'dealer_id_card_image_list' ),
				'business_license_image' => I ( 'business_license_image' ),
				'business_license_no' => I ( 'business_license_no' ),
				'finance_leader' => I ( 'finance_leader' ),
				'finance_leader_cellphone' => I ( 'finance_leader_cellphone' ),
				'invoice_address' => I ( 'invoice_address' ),
				'remark' => I ( 'remark' ),
				'sign_status' => I ( 'sign_status' ),
				'sign_date' => $sign_date,
				'contract_start_date' => $contract_start_date,
				'contract_end_date' => $contract_end_date,
				'status' => 1, // 待审核状态
				'update_user' => "whaleyinfo.order VRDealerController.updateDealerAction",
				'update_date' => date ( "y-m-d H:i:s", time () ) 
		);
		
		$upload_config = array (
				'maxSize' => 2 * 1024 * 1024,
				'rootPath' => './Public/Uploads/',
				'savePath' => 'vrdealer/' . $dealer_id . '/',
				'saveName' => array (
						'uniqid',
						'' 
				),
				'exts' => array (
						'jpg',
						'gif',
						'png',
						'jpeg' 
				),
				'subName' => '',
				'replace' => true 
		);
		$upload->autoSub = false;
		$upload = new \Think\Upload ( $upload_config ); // 实例化上传类
		
		$info = $upload->upload ();
		
		$upload_image_array = array ();
		
		foreach ( $info as $file ) {
			
			$key = $file ['key'];
			$upload_image_array ['path'] = $file ['savepath'] . $file ['savename'];
			switch ($key) {
				case 'dealer_id_card_image_list1' :
					
					if (empty ( $update_dealer_en ['dealer_id_card_image_list'] )) {
						$update_dealer_en ['dealer_id_card_image_list'] = $file ['savepath'] . $file ['savename'];
					} else {
						$id_images = explode ( "|", $update_dealer_en ['dealer_id_card_image_list'] );
						
						$update_dealer_en ['dealer_id_card_image_list'] = $file ['savepath'] . $file ['savename'] . '|' . $id_images [1];
					}
					
					break;
				case 'dealer_id_card_image_list2' :
					
					if (empty ( $update_dealer_en ['dealer_id_card_image_list'] )) {
						$update_dealer_en ['dealer_id_card_image_list'] = $file ['savepath'] . $file ['savename'];
					} else {
						$id_images = explode ( "|", $update_dealer_en ['dealer_id_card_image_list'] );
						$update_dealer_en ['dealer_id_card_image_list'] = $id_images [0] . '|' . $file ['savepath'] . $file ['savename'];
					}
					break;
				case 'business_license_image1' :
					
					$update_dealer_en ['business_license_image'] = $file ['savepath'] . $file ['savename'];
					break;
				
				case 'organization_image1' :
					
					$update_dealer_en ['organization_image'] = $file ['savepath'] . $file ['savename'];
					break;
				case 'tax_registration_image1' :
					
					$update_dealer_en ['tax_registration_image'] = $file ['savepath'] . $file ['savename'];
					break;
				case 'general_taxpayer_image1' :
					
					$update_dealer_en ['general_taxpayer_image'] = $file ['savepath'] . $file ['savename'];
					break;
				case 'account_opening_image1' :
					
					$update_dealer_en ['account_opening_image'] = $file ['savepath'] . $file ['savename'];
					break;
				
				default :
					break;
			}
		}
		
		// 上传完成
		
		// 添加dealer已及user信息
		
		DealerBiz::updateDealer ( $update_dealer_en );
		
		$info_msg = "VR渠道经销商信息更新成功！";
		redirect ( C ( 'AdminConfig.info_page' ) . '?type=202&msg=成功,' . $info_msg );
		
		return;
	}
	public function addDealerAction() {
		$dealer_id = DealerBiz::generateDealerId ();
		$user_id = DealerBiz::generateUserId ();
		$sign_date = I ( 'sign_date' );
		$contract_start_date = I ( 'contract_start_date' );
		$contract_end_date = I ( 'contract_end_date' );
		$status = I ( 'status' );
		
		if (empty ( $sign_date )) {
			$sign_date = null;
		} else {
			
			$sign_date = date ( "y-m-d H:i:s", strtotime ( $sign_date ) );
		}
		
		if (empty ( $contract_start_date )) {
			$contract_start_date = null;
		} else {
			
			$contract_start_date = date ( "y-m-d H:i:s", strtotime ( $contract_start_date ) );
		}
		
		if (empty ( $contract_end_date )) {
			$contract_end_date = null;
		} else {
			
			$contract_end_date = date ( "y-m-d H:i:s", strtotime ( $contract_end_date ) );
		}
		
		$add_dealer_en = array (
				'dealer_id' => $dealer_id,
				'dealer_name' => I ( 'dealer_name' ),
				'dealer_type' => I ( 'dealer_type' ),
				'dealer_channel' => I('dealer_channel'),
				'k3_code' => I ( 'k3_code' ),
				'user_id' => $user_id,
				'discount_rate' => I ( 'discount_rate' ),
				'business_leader' => I ( 'business_leader' ),
				'business_leader_cellphone' => I ( 'business_leader_cellphone' ),
				'area_name' => I ( 'area_name' ),
				'province_id' => I ( 'province_id' ),
				'province_name' => I ( 'province_name' ),
				'city_id' => I ( 'city_id' ),
				'city_name' => I ( 'city_name' ),
				'operation_pattern' => I ( 'operation_pattern' ),
				'registered_capital' => I ( 'registered_capital' ),
				'leal_person' => I ( 'leal_person' ),
				'bank_name' => I ( 'bank_name' ),
				'bank_no' => I ( 'bank_no' ),
				'tax_no' => I ( 'tax_no' ),
				'dealer_id_card_no' => I ( 'dealer_id_card_no' ),
				'business_license_no' => I ( 'business_license_no' ),
				'finance_leader' => I ( 'finance_leader' ),
				'finance_leader_cellphone' => I ( 'finance_leader_cellphone' ),
				'invoice_address' => I ( 'invoice_address' ),
				'remark' => I ( 'remark' ),
				'sign_status' => I ( 'sign_status' ),
				'sign_date' => $sign_date,
				'contract_start_date' => $contract_start_date,
				'contract_end_date' => $contract_end_date,
				'amount' => 0,
				'status' => 1,
				'create_user' => "whaleyinfo.order DealerBiz.addDealer",
				'create_date' => date ( "y-m-d H:i:s", time () ) 
		);
		
		$is_exists_dealer = M ( 'dealer_info' )->where ( "dealer_name='%s' and dealer_channel=2 ", I ( 'dealer_name' ) )->find ();
		if (! empty ( $is_exists_dealer )) {
			$error_msg = "VR渠道经销商已存在!";
			redirect ( C ( 'AdminConfig.error_info_page' ) . '?type=202&msg=,' . $error_msg );
			return;
		}
		
		$upload_config = array (
				'maxSize' => 0,
				'rootPath' => './Public/Uploads/',
				'savePath' => 'vrdealer/' . $dealer_id . '/',
				'saveName' => array (
						'uniqid',
						'' 
				),
				'exts' => array (
						'jpg',
						'gif',
						'png',
						'jpeg' 
				),
				'subName' => '',
				'replace' => true 
		);
		$upload->autoSub = false;
		$upload = new \Think\Upload ( $upload_config ); // 实例化上传类
		$info = $upload->upload ();
		
		$upload_image_array = array ();
		if (! $info) {
			// 转到注册失败页面
			// redirect(C('UrlConfig.common_error_url').'?type=11&param=缺少证件照片上传(图片过大，不要超过2mb)，'.$upload->getError());
			// $error_msg="请上传文件大小不超过2M并以jpg,gif,png,jpeg格式的图片！";
			$error_msg = $upload->getError ();
			redirect ( C ( 'AdminConfig.error_info_page' ) . '?type=202&msg=,' . $error_msg );
			return;
		}
		$req_image_list = array (
				"dealer_id_card_image_list1",
				"dealer_id_card_image_list2",
				"business_license_image1" 
		);
		
		$real_image_list = array ();
		
		foreach ( $info as $file ) {
			
			$key = $file ['key'];
			$upload_image_array ['path'] = $file ['savepath'] . $file ['savename'];
			switch ($key) {
				case 'dealer_id_card_image_list1' :
					
					array_push ( $real_image_list, "dealer_id_card_image_list1" );
					
					if (empty ( $add_dealer_en ['dealer_id_card_image_list'] )) {
						$add_dealer_en ['dealer_id_card_image_list'] = $file ['savepath'] . $file ['savename'];
					} else {
						$add_dealer_en ['dealer_id_card_image_list'] = $add_dealer_en ['dealer_id_card_image_list'] . '|' . $file ['savepath'] . $file ['savename'];
					}
					
					break;
				case 'dealer_id_card_image_list2' :
					
					array_push ( $real_image_list, "dealer_id_card_image_list2" );
					if (empty ( $add_dealer_en ['dealer_id_card_image_list'] )) {
						$add_dealer_en ['dealer_id_card_image_list'] = $file ['savepath'] . $file ['savename'];
					} else {
						$add_dealer_en ['dealer_id_card_image_list'] = $add_dealer_en ['dealer_id_card_image_list'] . '|' . $file ['savepath'] . $file ['savename'];
					}
					break;
				case 'business_license_image1' :
					
					array_push ( $real_image_list, "business_license_image1" );
					$add_dealer_en ['business_license_image'] = $file ['savepath'] . $file ['savename'];
					break;
				case 'organization_image1' :
					
					$add_dealer_en ['organization_image'] = $file ['savepath'] . $file ['savename'];
					break;
				case 'tax_registration_image1' :
					
					$add_dealer_en ['tax_registration_image'] = $file ['savepath'] . $file ['savename'];
					break;
				case 'general_taxpayer_image1' :
					
					$add_dealer_en ['general_taxpayer_image'] = $file ['savepath'] . $file ['savename'];
					break;
				case 'account_opening_image1' :
					
					$add_dealer_en ['account_opening_image'] = $file ['savepath'] . $file ['savename'];
					break;
				default :
					break;
			}
		}
		
		// 上传完成
		
		// 判断必须上传的图片有没有全部上传
		
		// echo $real_image_list[0];
		// return;
		
		foreach ( $req_image_list as $req ) {
			
			$flag = FALSE;
			foreach ( $real_image_list as $real ) {
				if ($req == $real) {
					$flag = True;
					break;
				}
			}
			
			if (! $flag) {
				
				switch ($req) {
					
					case 'dealer_id_card_image_list1' :
						
						$error_msg = "请上传身份证正面!";
						redirect ( C ( 'AdminConfig.error_info_page' ) . '?type=202&msg=,' . $error_msg );
						return;
						break;
					case 'dealer_id_card_image_list2' :
						
						$error_msg = "请上传身份证反面!";
						redirect ( C ( 'AdminConfig.error_info_page' ) . '?type=202&msg=,' . $error_msg );
						return;
						break;
					case 'business_license_image1' :
						$error_msg = "请上传营业执照图片!";
						redirect ( C ( 'AdminConfig.error_info_page' ) . '?type=202&msg=,' . $error_msg );
						return;
						break;
					
					default :
						break;
				}
				
				break;
			}
		}
		
		// 添加dealer已及user信息
		DealerBiz::addDealer ( $add_dealer_en );
		
		// 查看登录用户是否为业务员
		$sale_id = cookie ( 'login_user_id' );
		$sale_en = M ( 'sale_user' )->where ( "sale_name='%s'", $sale_id )->find ();
		// 业务员逻辑
		if (! empty ( $sale_en )) {
			
			SaleBiz::addDealerSaleRelationship ( $sale_en ['sale_id'], $dealer_id );
			$log = array (
					'sale_id' => $sale_id,
					'dealer_id' => $dealer_id,
					'operation' => 'add',
					'operator_id' => cookie ( 'login_user_id' ),
					'create_user' => "whaleyinfo.order VRDealerController.addDealerAction",
					'create_date' => date ( "y-m-d H:i:s", time () ) 
			);
			
			SaleBiz::addSaleDealerLog ( $log );
		}
		
		$log = array (
				
				'dealer_id' => $dealer_id,
				'operation' => 'add',
				'operator_id' => cookie ( 'login_user_id' ),
				'create_user' => "whaleyinfo.order VRDealerController.addDealerAction",
				'create_date' => date ( "y-m-d H:i:s", time () ) 
		);
		
		SaleBiz::addSaleDealerLog ( $log );
		
		$info_msg = "VR渠道经销商信息添加成功！";
		redirect ( C ( 'AdminConfig.info_page' ) . '?type=202&msg=成功,' . $info_msg );
		
		return;
	}
	public function dealerInfo() {
		// 查看登录用户是否为业务员
		$sale_id = cookie ( 'login_user_id' );
		// $sale_id = 'USR11147258';
		$sale_en = M ( 'sale_user' )->where ( "sale_name='%s'", $sale_id )->find ();
		// 业务员逻辑
		if (! empty ( $sale_en )) {
			
			$this->assign ( "isBussiness", 1 );
		}
		
		$this->assign ( "page_name", "VR渠道经销商信息列表" );
		$this->display ( 'vrdealer/dealerinfo' );
	}
	public function detailDealer() {
		$dealer_id = I ( 'dealer_id' );
		// $dealer_id = "DL100001";
		$dealer_en = M ( 'dealer_info' )->where ( "dealer_id='%s'", $dealer_id )->find ();
		
		if (empty ( $dealer_en )) {
			
			$error_msg = "该VR渠道经销商信息不存在,请检查";
			redirect ( C ( 'AdminConfig.error_info_page' ) . '?type=202&msg=,' . $error_msg );
			return;
		} else {
			
			$id_images = explode ( "|", $dealer_en ['dealer_id_card_image_list'] );
			$dealer_en ['id_1'] = "/Public/Uploads/" . $id_images [0];
			$dealer_en ['id_2'] = "/Public/Uploads/" . $id_images [1];
			$dealer_en ['id_3'] = "/Public/Uploads/" . $dealer_en ['business_license_image'];
			$dealer_en ['id_4'] = "/Public/Uploads/" . $dealer_en ['organization_image'];
			$dealer_en ['id_5'] = "/Public/Uploads/" . $dealer_en ['tax_registration_image'];
			$dealer_en ['id_6'] = "/Public/Uploads/" . $dealer_en ['general_taxpayer_image'];
			$dealer_en ['id_7'] = "/Public/Uploads/" . $dealer_en ['account_opening_image'];
			$this->assign ( "dealer_en", $dealer_en );
			$this->assign ( "page_name", "VR渠道经销商信息更新" );
			$this->display ( "vrdealer/detaildealer" );
		}
	}
	public function dealerbelongstosale() {
		$sale_id = I ( 'sale_id' );
		$sale_en = M ( 'sale_user' )->where ( "sale_id='%s'", $dealer_id )->find ();
		
		if (empty ( $sale_en )) {
			
			$error_msg = "该业务员不存在,请检查";
			redirect ( C ( 'AdminConfig.error_info_page' ) . '?type=505&msg=,' . $error_msg );
			return;
		} else {
			
			$this->assign ( "sale_id", $sale_id );
			$this->assign ( "page_name", "业务员名下所有VR渠道经销商" );
			$this->display ( "vrsale/dealersbelongstosale" );
		}
	}
	public function dealerinfoAuditPass() {
		$dealer_id = I ( 'dealer_id' );
		// $dealer_id = "DL100001";
		$dealer_en = M ( 'dealer_info' )->where ( "dealer_id='%s'", $dealer_id )->find ();
		
		if (empty ( $dealer_en )) {
			
			$error_msg = "该VR渠道经销商信息不存在,请检查";
			redirect ( C ( 'AdminConfig.error_info_page' ) . '?type=202&msg=,' . $error_msg );
			return;
		} else {
			
			M ( 'dealer_info' )->where ( "dealer_id='%s'", $dealer_id )->setField ( "status", 2 );
			
			$info_msg = "VR渠道经销商信息审批通过！";
			redirect ( C ( 'AdminConfig.info_page' ) . '?type=202&msg=成功,' . $info_msg );
			return;
		}
	}
	public function dealerinfoAuditFail() {
		$dealer_id = I ( 'dealer_id' );
		$remark = I ( 'remark' );
		
		// $dealer_id = "DL100001";
		$dealer_en = M ( 'dealer_info' )->where ( "dealer_id='%s'", $dealer_id )->find ();
		
		if (empty ( $dealer_en )) {
			
			$error_msg = "该VR渠道经销商信息不存在,请检查";
			redirect ( C ( 'AdminConfig.error_info_page' ) . '?type=202&msg=,' . $error_msg );
			return;
		} elseif (empty ( $remark )) {
			$error_msg = "VR渠道经销商信息审批失败,必须给出原因!";
			redirect ( C ( 'AdminConfig.error_info_page' ) . '?type=202&msg=,' . $error_msg );
			return;
		} 

		else {
			// 审批失败
			
			$update_dealer_info = array (
					'status' => 3,
					'remark' => $dealer_en ['remark'] . "|" . $remark 
			)
			;
			
			M ( 'dealer_info' )->where ( "dealer_id='%s'", $dealer_id )->save ( $update_dealer_info );
			
			$error_msg = "" . $remark;
			redirect ( C ( 'AdminConfig.error_info_page' ) . '?type=202&msg=,' . $error_msg );
			return;
		}
	}
	public function showDealer() {
		$dealer_id = I ( 'dealer_id' );
		// $dealer_id = "DL100001";
		$dealer_id = I ( 'dealer_id' );
		// $dealer_id = "DL100001";
		$dealer_info_en = M ( 'dealer_info' )->where ( "dealer_id='%s'", $dealer_id )->find ();
		
		if (empty ( $dealer_info_en )) {
			
			$error_msg = "该VR渠道经销商信息不存在,请检查";
			redirect ( C ( 'AdminConfig.error_info_page' ) . '?type=202&msg=,' . $error_msg );
			return;
		} else {
			
			$dealer_info_en ['dealer_type_text'] = StringBiz::getDealerTypeRevenueStatusText ( $dealer_info_en ['dealer_type'] );
			$dealer_info_en ['status_text'] = StringBiz::getDealerStatusRevenueStatusText ( $dealer_info_en ['status'] );
			$dealer_info_en ['sign_status_text'] = StringBiz::getDealerSignStatusRevenueStatusText ( $dealer_info_en ['sign_status'] );
			$id_images = explode ( "|", $dealer_info_en ['dealer_id_card_image_list'] );
			$dealer_info_en ['id_1'] = "/Public/Uploads/" . $id_images [0];
			$dealer_info_en ['id_2'] = "/Public/Uploads/" . $id_images [1];
			$dealer_info_en ['id_3'] = "/Public/Uploads/" . $dealer_info_en ['business_license_image'];
			$dealer_info_en ['id_4'] = "/Public/Uploads/" . $dealer_info_en ['organization_image'];
			$dealer_info_en ['id_5'] = "/Public/Uploads/" . $dealer_info_en ['tax_registration_image'];
			$dealer_info_en ['id_6'] = "/Public/Uploads/" . $dealer_info_en ['general_taxpayer_image'];
			$dealer_info_en ['id_7'] = "/Public/Uploads/" . $dealer_info_en ['account_opening_image'];
			$user_addr_en = M ( 'user_address' )->where ( "user_id='%s'", $dealer_info_en ['user_id'] )->select ();
			$this->assign ( "dealer_info_en", $dealer_info_en );
			$this->assign ( "user_addr_en", $user_addr_en );
			$this->assign ( "page_name", "VR渠道经销商信息明细" );
			$this->display ( 'vrdealer/showdealer' );
		}
	}
	public function dealerOrderCart() {
		$lst_product = M ( 'sku', '', 'DB_whaley_product' )->where ( "sku_channel =10 and status=1" )->select ();
		
		$this->assign ( "lst_product", $lst_product );
		$this->assign ( 'page_name', 'VR渠道经销商订单录入' );
		
		$address_user_id = I ( 'address_user_id' );
		
		if (! empty ( $address_user_id )) {
			$dealer_info_en = M ( 'dealer_info' )->where ( "user_id='%s'", $address_user_id )->find ();
			$this->assign ( 'param_dealer_id', json_encode ( $dealer_info_en ['dealer_id'] ) );
			$this->assign ( 'param_user_id', json_encode ( $dealer_info_en ['user_id'] ) );
			$this->assign ( 'param_dealer_name', json_encode ( $dealer_info_en ['dealer_name'] ) );
		} else {
			$this->assign ( 'param_dealer_id', json_encode ( '' ) );
			$this->assign ( 'param_user_id', json_encode ( '' ) );
			$this->assign ( 'param_dealer_name', json_encode ( '' ) );
		}
		
		$this->display ( 'vrdealer/dealerOrderCart' );
	}
	public function dealerinfosearch() {
		$dealer_name = I ( 'dealer_name' );
		$dealer_id = I ( 'dealer_id' );
		
		$con = array ();
		
		array_push ( $con, "status=2" );
		
		if (! empty ( $dealer_name )) {
			array_push ( $con, "dealer_name like '%$dealer_name%'" );
		}
		if (! empty ( $dealer_id )) {
			array_push ( $con, "dealer_id like '%$dealer_id%'" );
		}
		$lst_dealer_info = M ( 'dealer_info' )->where ( join ( " AND ", $con ) )->select ();
		
		$sale_id = cookie ( 'login_user_id' );
		$sale_en = M ( 'sale_user' )->where ( "sale_name='%s'", $sale_id )->find ();
		if ($sale_en == null) {
		} else {
			$result = SaleBiz::queryDealerBySaleAndPara ( $sale_en ['sale_id'], $dealer_name, '', '', '', null, 2,'',2, 9999, 1 );
			$lst_dealer_info = $result ['datas'];
		}
		
		$this->assign ( "lst_dealer_info", $lst_dealer_info );
		
		$this->display ( 'vrdealer/dealerinfosearch' );
	}
	public function dealerOrderCheckout() {
		$dealer_id = I ( 'dealer_id' );
		$select_address_id = I ( 'select_address_id' );
		$sku_list_str = I ( 'sku_list_str' );
		$payment_type = I ( 'payment_type' );
		
		$dealer_en = M ( 'dealer_info' )->where ( "dealer_id='$dealer_id'" )->find ();
		$user_address = M ( 'user_address' )->where ( "address_id='$select_address_id'" )->find ();
		
		$lst_goods_info = explode ( "|", $sku_list_str );
		
		$goods_total_amount = 0;
		
		$discount_total_amount = 0;
		
		$total_qty = 0;
		
		$lst_product = array ();
		
		// 计算商品总数，折扣总价
		foreach ( $lst_goods_info as $goods_info ) {
			
			$sku_str = explode ( ",", $goods_info );
			$sku_id = $sku_str [0];
			$qty = intval ( $sku_str [1] );
			if ($qty > 0) {
				$sku_en = M ( 'sku', '', 'DB_whaley_product' )->where ( "sku_id='$sku_id'" )->find ();
				$sku_en ['qty'] = $qty;
				
				$sale_price = $sku_en ['sale_price'];
				$discount_price = $sale_price;
				if ($sku_en ['is_rebate'] == '1') {
					$discount_price = $sale_price - $sale_price * $dealer_en ['discount_rate'];
				}
				
				$discount_price = round($discount_price,2);
				$sku_en ['discount_price'] = $discount_price;
				$sku_en ['discount_total_price'] = $discount_price * $qty;
				$total_qty = $total_qty + $qty;
				
				$goods_total_amount = $goods_total_amount + $sale_price * $qty;
				
				$discount_total_amount = $discount_total_amount + $discount_price * $qty;
				
				array_push ( $lst_product, $sku_en );
			}
		}
		
		$this->assign ( "total_qty", $total_qty );
		$this->assign ( "goods_total_amount", $goods_total_amount );
		$this->assign ( "discount_total_amount", $discount_total_amount );
		$this->assign ( "discount_amount", $goods_total_amount - $discount_total_amount );
		$this->assign ( "lst_product", $lst_product );
		$this->assign ( "dealer_en", $dealer_en );
		$this->assign ( "user_address", $user_address );
		$this->assign ( "dealer_id", $dealer_id );
		$this->assign ( "select_address_id", $select_address_id );
		$this->assign ( 'payment_type', $payment_type );
		$payment_type_text = CommonBiz::changePaymentType ( $payment_type );
		$this->assign ( 'payment_type_text', $payment_type_text );
		$this->assign ( "lst_sku", $sku_list_str );
		
		$this->display ( 'vrdealer/dealerOrderCheckout' );
	}
	public function dealerOrderSuccess() {
		$order_id = I ( 'order_id' );
		$this->assign ( "order_id", $order_id );
		$this->display ( 'vrdealer/dealerOrderSuccess' );
	}
	public function dealerOrderList() {
		$this->display ( 'vrdealer/dealerOrderInfo' );
	}
	public function dealerOrderDetail() {
		$this->display ( 'vrdealer/dealerOrderDetail' );
	}
	
	/**
	 * 上传page
	 */
	public function dealerUploadPage() {
		$this->display ( 'vrdealer/dealerOrderUpload' );
	}
	/**
	 * VR渠道经销商订单上传
	 */
	public function dealerStockOrderImportAction() {
		$upload_config = array (
				'maxSize' => 0,
				'rootPath' => './Public/Uploads/',
				'savePath' => 'stockOrderImport/',
				'saveName' => 'stockOrderImport',
				'exts' => array (
						'xls' 
				),
				'subName' => '',
				'replace' => true 
		);
		
		$upload = new \Think\Upload ( $upload_config ); // 实例化上传类
		$upload->autoSub = false;
		
		$info = $upload->upload ();
		if (! $info) {
			// 上传错误提示错误信息
			redirect ( 'dealerOrderUploadResult?msg=上传失败！' . $upload->getError () );
		} else {
			// 上传成功
			// 读取csv文件将相关数据写入数据库
			// $xls_file = fopen('./Public/Uploads/stockOrderImport/stockOrderImport.xls','r');
			$err_msg = DealerBiz::stockOrderImport ( './Public/Uploads/stockOrderImport/stockOrderImport.xls' );
			if (empty ( $err_msg )) {
				redirect ( 'dealerOrderUploadResult?msg=已成功上传并处理！' );
			} else {
				redirect ( "dealerOrderUploadResult?msg=$err_msg！" );
			}
		}
	}
	public function dealerOrderUploadResult() {
		$this->assign ( 'msg', I ( 'msg' ) );
		$this->display ( 'vrdealer/dealerOrderUploadResult' );
	}
	public function stockOrderImportResult() {
		$this->assign ( 'msg', I ( 'msg' ) );
		$this->display ( 'order/stockOrderImportResult' );
	}
	public function shipOrderExcel() {
		// 参数处理
		$params_en = array (
				'dealer_id' => I ( 'dealer_id' ),
				'dealer_name' => I ( 'dealer_name' ),
				'dealer_type' => I ( 'dealer_type' ),
				'dealer_channel'=>I('dealer_channel'),
				'order_id' => I ( 'order_id' ),
				'order_status' => I ( 'order_status' ),
				'payment_status' => I ( 'payment_status' ), 
				'order_startdate' =>I('start_date'),
				'order_enddate'  =>I('end_date')
		);
		
		Vendor ( 'phpExcel.PHPExcel' );
		$objPHPExcel = OrderBiz::shipOrderExcel ( $params_en );
		
// 		echo $objPHPExcel.I('start_date');
// 		return ;
		// 输出到浏览器
		// $objWriter = new \PHPExcel_Writer_Excel2007($objPHPExcel);
		$objWriter = new \PHPExcel_Writer_Excel5 ( $objPHPExcel );
		
		header ( "Pragma: public" );
		header ( "Expires: 0" );
		header ( "Cache-Control:must-revalidate, post-check=0, pre-check=0" );
		header ( "Content-Type:application/force-download" );
		header ( "Content-Type:application/vnd.ms-execl" );
		header ( "Content-Type:application/octet-stream" );
		header ( "Content-Type:application/download" );
		header ( 'Content-Disposition:attachment;filename="result.xls"' );
		header ( "Content-Transfer-Encoding:binary" );
		$objWriter->save ( 'php://output' );
	}
	public function dealersalseManagerAudit() {
		$order_id = I ( 'order_id' );
		$audit_type = I ( 'audit_type' );
		$errorMsg = "";
		$successMsg = "";
		
		$order_en = M ( 'order_info' )->where ( "order_id='%s'", $order_id )->find ();
		
		if (empty ( $order_en )) {
			$errorMsg = "订单不存在，请检查订单号！";
			$this->assign ( "order_id", $order_id );
			$this->assign ( "errorMsg", $errorMsg );
			$this->display ( "vrdealer/dealerauditerror" );
			return;
		}
		
		switch ($audit_type) {
			
			case 14 :
				// 销售管理部审批支付凭证已上传的订单
				if ($order_en ['order_status'] == 23) {
					// 改为14 order_info status, insert order_status_change_log
					M ( 'order_info' )->where ( "order_id='%s'", $order_id )->setField ( "order_status", 14 );
					$order_change_log = array (
							"order_id" => $order_id,
							"order_status" => 14,
							"order_status_text" => '销售管理部已审批',
							"create_user" => 'whaleyinfo.order dealer.dealerAudit',
							"create_date" => date ( "y-m-d H:i:s", time () ) 
					);
					M ( 'order_status_change_log' )->data ( $order_change_log )->add ();
					
					$successMsg = "销售管理部审批成功！";
					$this->assign ( "order_id", $order_id );
					$this->assign ( "successMsg", $successMsg );
					MailBiz::callMailApi ( C ( 'VREmailConfig.AccountReceiverList' ), 'VR渠道经销商渠道订单' . $order_id . C ( 'VREmailConfig.AccountReceiverMailSubject' ), 'VR渠道经销商渠道订单' . $order_id . C ( 'VREmailConfig.AccountReceiverMailSubject' ) );
					$this->display ( "vrdealer/dealerauditsuccess" );
					
					return;
				} else {
					
					$errorMsg = "订单状态非法，请检查！";
					$this->assign ( "order_id", $order_id );
					$this->assign ( "errorMsg", $errorMsg );
					$this->display ( "vrdealer/dealerauditerror" );
					return;
				}
			
			default :
				$errorMsg = "操作非法，不符合审批流程！";
				$this->assign ( "order_id", $order_id );
				$this->assign ( "errorMsg", $errorMsg );
				$this->display ( "vrdealer/dealerauditerror" );
				return;
				break;
		}
	}
	public function dealerfinanceAudit() {
		$order_id = I ( 'order_id' );
		$audit_type = I ( 'audit_type' );
		$errorMsg = "";
		$successMsg = "";
		
		$order_en = M ( 'order_info' )->where ( "order_id='%s'", $order_id )->find ();
		
		if (empty ( $order_en )) {
			$errorMsg = "订单不存在，请检查订单号！";
			$this->assign ( "order_id", $order_id );
			$this->assign ( "errorMsg", $errorMsg );
			$this->display ( "vrdealer/dealerauditerror" );
			return;
		}
		
		switch ($audit_type) {
			
			case 15 :
				// 财务只处理订单状态为14即销售管理部已审批
				if ($order_en ['order_status'] == 14) {
					// 改为15order_info status=>15, paymentstatus =>1 ;order_payment paymentstatus =>1
					// ;insert order_status_change_log
					
					$order_fields = array (
							"order_status" => 15,
							"payment_status" => 1 
					);
					
					M ( 'order_info' )->where ( "order_id='%s'", $order_id )->data ( $order_fields )->save ();
					
					$payment_fields = array(
							
						"payment_status"	 =>1,
						"payment_date" =>date ( "y-m-d H:i:s", time ())					
					);
					M ( 'order_payment' )->where ( "order_id='%s'", $order_id )->data($payment_fields)->save();
					$order_change_log = array (
							"order_id" => $order_id,
							"order_status" => 15,
							"order_status_text" => '财务经理已审批',
							"create_user" => 'whaleyinfo.order dealer.dealerAudit',
							"create_date" => date ( "y-m-d H:i:s", time () ) 
					);
					M ( 'order_status_change_log' )->data ( $order_change_log )->add ();
					
					// 财务经理审批完默认就是已经支付
					M ( 'order_info' )->where ( "order_id='%s'", $order_id )->setField ( "order_status", 22 );
					$order_change_log1 = array (
							"order_id" => $order_id,
							"order_status" => 22,
							"order_status_text" => '已支付',
							"create_user" => 'whaleyinfo.order dealer.dealerAudit',
							"create_date" => date ( "y-m-d H:i:s", time () ) 
					);
					M ( 'order_status_change_log' )->data ( $order_change_log1 )->add ();
					$successMsg = "财务经理审批成功！";
					$this->assign ( "order_id", $order_id );
					$this->assign ( "successMsg", $successMsg );
					$this->display ( "vrdealer/dealerauditsuccess" );
					MailBiz::callMailApi ( C ( 'VREmailConfig.LogisticsReceiverList' ), 'VR渠道经销商渠道订单' . $order_id . C ( 'VREmailConfig.LogisticsReceiverMailSubject' ), 'VR渠道经销商渠道订单' . $order_id . C ( 'VREmailConfig.LogisticsReceiverMailSubject' ) );
					return;
				} else {
					
					$errorMsg = "订单状态非法，请检查";
					$this->assign ( "order_id", $order_id );
					$this->assign ( "errorMsg", $errorMsg );
					$this->display ( "vrdealer/dealerauditerror" );
					return;
				}
			default :
				$errorMsg = "操作非法，不符合审批流程！";
				$this->assign ( "order_id", $order_id );
				$this->assign ( "errorMsg", $errorMsg );
				$this->display ( "vrdealer/dealerauditerror" );
				return;
				break;
		}
	}
	public function dealerAmountList() {
		$this->assign ( "page_name", "VR渠道经销商余额列表" );
		$this->display ( "vrdealer/dealeramountchange" );
	}
	public function dealerAmountAuditList() {
		$this->assign ( "page_name", "VR渠道经销商余额审批列表" );
		$this->display ( "vrdealer/dealeramountauditlist" );
	}
	public function showDealerAmountRecharge() {
		$dealer_recharge_id = I ( 'dealer_recharge_id' );
		$recharge_en = M ( 'dealer_recharge' )->where ( "dealer_recharge_id='%s'", $dealer_recharge_id )->find ();
		if (empty ( $recharge_en )) {
			$error_msg = "该申请记录不存在!";
			redirect ( C ( 'AdminConfig.error_info_page' ) . '?type=202&msg=,' . $error_msg );
			return;
		}
		$dealer_en = M ( 'dealer_info' )->where ( "dealer_id='%s'", $recharge_en ['dealer_id'] )->find ();
		
		if (empty ( $dealer_en )) {
			
			$error_msg = "VR渠道经销商不存在!";
			redirect ( C ( 'AdminConfig.error_info_page' ) . '?type=202&msg=,' . $error_msg );
			return;
		}
		
		$recharge_en ['recharge_status_text'] = StringBiz::getDealerRechargeTypeRevenueStatusText ( $recharge_en ['recharge_status'] );
		$recharge_en ['amount_kind_text'] = StringBiz::getDealerAmountKindRevenueStatusText ( $recharge_en ['amount_kind'] );
		$recharge_en ['amount_type_text'] = StringBiz::getDealerAmountTypeRevenueStatusText ( $recharge_en ['amount_type'] );
		$recharge_en ['identify_image'] = "/Public/Uploads/" . $recharge_en ['identify_image'];
		
		$this->assign ( "page_name", "VR渠道经销商余额审批详情" );
		$this->assign ( "dealer_recharge_en", $recharge_en );
		$this->display ( "vrdealer/showDealerAmountRecharge" );
	}
	public function dealerAmountApply() {
		$dealer_id = I ( 'dealer_id' );
		
		$is_exists_dealer = M ( 'dealer_info' )->where ( "dealer_id='%s'", $dealer_id )->find ();
		if (empty ( $is_exists_dealer )) {
			$error_msg = "VR渠道经销商不存在!";
			redirect ( C ( 'AdminConfig.error_info_page' ) . '?type=202&msg=,' . $error_msg );
			return;
		}
		
		$this->assign ( "dealer_en", $is_exists_dealer );
		$this->display ( "vrdealer/dealeramountapply" );
		return;
	}
	public function dealerAmountApplyAction() {
		
		// 查看登录用户是否为业务员
		$sale_name = cookie ( 'login_user_id' );
		$sale_en = M ( 'sale_user' )->where ( "sale_name='%s'", $sale_name )->find ();
		// 业务员登录
		if (! empty ( $sale_en )) {
			
			$role_type = 2;
			$role_id = $sale_en ['sale_id'];
			$role_name = $sale_en ['sale_name'];
		} else {
			// 管理员角色
			$role_type = 1;
			$whaley_user = M ( 'whaley_user', '', 'DB_whaley_system' )->where ( "user_name='%s'", $sale_name )->find ();
			$role_id = $whaley_user ['user_id'];
			$role_name = $whaley_user ['user_name'];
		}
		
		$dealer_id = I ( 'dealer_id' );
		$dealer_en = M ( 'dealer_info' )->where ( "dealer_id='%s'", $dealer_id )->find ();
		$dealer_recharge_id = DealerBiz::generateDealerAmountChargeId ();
		$add_dealer_recharge = array (
				'dealer_recharge_id' => $dealer_recharge_id,
				'dealer_id' => $dealer_id,
				'dealer_name' => I ( 'dealer_name' ),
				'audit_user_name' => cookie ( 'login_user_id' ),
				'amount_kind' => I ( 'amount_kind' ),
				'amount_type' => I ( 'amount_type' ),
				'amount' => I ( 'amount' ),
				'recharge_status' => 1,
				'remark' => I ( 'remark' ),
				'create_user' => "whaleyinfo.order VRDealerController.dealerAmountApplyAction",
				'create_date' => date ( "y-m-d H:i:s", time () ) 
		);
		
		$add_dealer_recharge_log = array (
				'change_id' => DealerBiz::generateDealerAmountChangeId (),
				'dealer_recharge_id' => $dealer_recharge_id,
				'dealer_id' => $dealer_id,
				'dealer_name' => I ( 'dealer_name' ),
				'recharge_status_text' => StringBiz::getDealerRechargeTypeRevenueStatusText ( 1 ),
				'recharge_status' => 1,
				'role_type' => $role_type,
				'role_id' => $role_id,
				'role_name' => $role_name,
				'remark' => I ( 'remark' ),
				'create_user' => "whaleyinfo.order VRDealerController.dealerAmountApplyAction",
				'create_date' => date ( "y-m-d H:i:s", time () ) 
		);
		
		$upload_config = array (
				'maxSize' => 0,
				'rootPath' => './Public/Uploads/',
				'savePath' => 'vrdealer/' . $dealer_id . '/',
				'saveName' => array (
						'uniqid',
						'' 
				),
				'exts' => array (
						'jpg',
						'gif',
						'png',
						'jpeg' 
				),
				'subName' => '',
				'replace' => true 
		);
		$upload->autoSub = false;
		$upload = new \Think\Upload ( $upload_config ); // 实例化上传类
		$info = $upload->upload ();
		
		if (! $info) {
			// 转到注册失败页面
			// redirect(C('UrlConfig.common_error_url').'?type=11&param=缺少证件照片上传(图片过大，不要超过2mb)，'.$upload->getError());
			// $error_msg="请上传文件大小不超过2M并以jpg,gif,png,jpeg格式的图片！";
			$error_msg = $upload->getError ();
			redirect ( C ( 'AdminConfig.error_info_page' ) . '?type=202&msg=,' . $error_msg );
			return;
		}
		
		foreach ( $info as $file ) {
			
			$key = $file ['key'];
			switch ($key) {
				case 'identify_image' :
					$add_dealer_recharge ['identify_image'] = $file ['savepath'] . $file ['savename'];
					break;
				default :
					break;
			}
		}
		
		M ( 'dealer_recharge' )->add ( $add_dealer_recharge );
		M ( 'dealer_recharge_status_change_log' )->add ( $add_dealer_recharge_log );
		
		$error_msg = "余额申请已提交!";
		redirect ( C ( 'AdminConfig.info_page' ) . '?type=202&is_show_button=2&msg=' . $error_msg );
		return;
	}
	public function saleDepartmentAudit() {
		
		$dealer_recharge_id = I ( 'dealer_recharge_id' );
		$type = I ( 'type' );
		$dealer_recharge_en = M ( 'dealer_recharge' )->where ( "dealer_recharge_id='%s'", $dealer_recharge_id )->find ();
		if (empty ( $dealer_recharge_en )) {
			
			$error_msg = "记录不存在,请检查!";
			redirect ( C ( 'AdminConfig.error_info_page' ) . '?type=202&is_show_button=2&msg=,' . $error_msg );
			return;
		}
		
		if ($dealer_recharge_en ['recharge_status'] != 1) {
			
			$error_msg = "充值申请状态非法,请检查!";
			redirect ( C ( 'AdminConfig.error_info_page' ) . '?type=202&is_show_button=2&msg=,' . $error_msg );
			return;
		}
		
		// 查看登录用户是否为业务员
		$sale_name = cookie ( 'login_user_id' );
		$sale_en = M ( 'sale_user' )->where ( "sale_name='%s'", $sale_name )->find ();
		// 业务员登录
		if (! empty ( $sale_en )) {
			
			$role_type = 2;
			$role_id = $sale_en ['sale_id'];
			$role_name = $sale_en ['sale_name'];
		} else {
			// 管理员角色
			$role_type = 1;
			$whaley_user = M ( 'whaley_user', '', 'DB_whaley_system' )->where ( "user_name='%s'", $sale_name )->find ();
			$role_id = $whaley_user ['user_id'];
			$role_name = $whaley_user ['user_name'];
		}
		
		switch ($type) {
			
			case 1 :
				// 销售管理部审批成功 1->3
				M ( 'dealer_recharge' )->where ( "dealer_recharge_id='%s'", $dealer_recharge_id )->setField ( "recharge_status", 3 );
				$add_dealer_recharge_log = array (
						'change_id' => DealerBiz::generateDealerAmountChangeId (),
						'dealer_recharge_id' => $dealer_recharge_id,
						'dealer_id' => $dealer_recharge_en ['dealer_id'],
						'dealer_name' => $dealer_recharge_en ['dealer_name'],
						'recharge_status_text' => StringBiz::getDealerRechargeTypeRevenueStatusText ( 3 ),
						'recharge_status' => 3,
						'role_type' => $role_type,
						'role_id' => $role_id,
						'role_name' => $role_name,
						'remark' => I ( 'remark' ),
						'create_user' => "whaleyinfo.order VRDealerController.saleDepartmentAudit",
						'create_date' => date ( "y-m-d H:i:s", time () ) 
				);
				M ( 'dealer_recharge_status_change_log' )->add ( $add_dealer_recharge_log );
				$error_msg = "销售管理部审批成功!";
				redirect ( C ( 'AdminConfig.info_page' ) . '?type=202&is_show_button=2&msg=' . $error_msg );
				return;
			case 2 :
				// 销售管理部审批失败 1->11
				$remark = I('remark');
				if(empty($remark)){
					
					$error_msg = "请输入审批人意见!";
					redirect ( C ( 'AdminConfig.error_info_page' ) . '?type=202&is_show_button=2&msg=,' . $error_msg );
					return;
				}
				M ( 'dealer_recharge' )->where ( "dealer_recharge_id='%s'", $dealer_recharge_id )->setField ( "recharge_status", 11 );
				$add_dealer_recharge_log = array (
						'change_id' => DealerBiz::generateDealerAmountChangeId (),
						'dealer_recharge_id' => $dealer_recharge_id,
						'dealer_id' => $dealer_recharge_en ['dealer_id'],
						'dealer_name' => $dealer_recharge_en ['dealer_name'],
						'recharge_status_text' => StringBiz::getDealerRechargeTypeRevenueStatusText ( 11 ),
						'recharge_status' => 11,
						'role_type' => $role_type,
						'role_id' => $role_id,
						'role_name' => $role_name,
						'remark' => I ( 'remark' ),
						'create_user' => "whaleyinfo.order VRDealerController.saleDepartmentAudit",
						'create_date' => date ( "y-m-d H:i:s", time () ) 
				);
				M ( 'dealer_recharge_status_change_log' )->add ( $add_dealer_recharge_log );
				$error_msg = "销售管理部审批已拒绝!";
				redirect ( C ( 'AdminConfig.info_page' ) . '?type=202&is_show_button=2&msg=' . $error_msg );
				return;
			default :
				$error_msg = "操作非法,请检查!";
				redirect ( C ( 'AdminConfig.error_info_page' ) . '?type=202&is_show_button=2&msg=,' . $error_msg );
				return;
		}
	}
	public function financeDepartmentAudit() {
		
		$dealer_recharge_id = I ( 'dealer_recharge_id' );
		$type = I ( 'type' );
		$dealer_recharge_en = M ( 'dealer_recharge' )->where ( "dealer_recharge_id='%s'", $dealer_recharge_id )->find ();
		if (empty ( $dealer_recharge_en )) {
				
			$error_msg = "记录不存在,请检查!";
			redirect ( C ( 'AdminConfig.error_info_page' ) . '?type=202&is_show_button=2&msg=,' . $error_msg );
			return;
		}
		
		if ($dealer_recharge_en ['recharge_status'] != 3) {
				
			$error_msg = "充值申请状态非法,请检查!";
			redirect ( C ( 'AdminConfig.error_info_page' ) . '?type=202&is_show_button=2&msg=,' . $error_msg );
			return;
		}
		
		// 查看登录用户是否为业务员
		$sale_name = cookie ( 'login_user_id' );
		$sale_en = M ( 'sale_user' )->where ( "sale_name='%s'", $sale_name )->find ();
		// 业务员登录
		if (! empty ( $sale_en )) {
				
			$role_type = 2;
			$role_id = $sale_en ['sale_id'];
			$role_name = $sale_en ['sale_name'];
		} else {
			// 管理员角色
			$role_type = 1;
			$whaley_user = M ( 'whaley_user', '', 'DB_whaley_system' )->where ( "user_name='%s'", $sale_name )->find ();
			$role_id = $whaley_user ['user_id'];
			$role_name = $whaley_user ['user_name'];
		}
		
		switch ($type) {
				
			case 3 :
				// 财务部审批成功 3->4
				M ( 'dealer_recharge' )->where ( "dealer_recharge_id='%s'", $dealer_recharge_id )->setField ( "recharge_status", 4 );
				$dealer_en = M('dealer_info')->where("dealer_id='%s'",$dealer_recharge_en['dealer_id'])->find();
				//插入余额表
				$dealer_amount = array(
						"dealer_amount_id"	=> DealerBiz::generateDealerAmountId(),
						"dealer_id" =>$dealer_recharge_en['dealer_id'],
						"dealer_name" =>$dealer_recharge_en['dealer_name'],
						"amount_kind" =>$dealer_recharge_en['amount_kind'],
						"amount_type" =>$dealer_recharge_en['amount_type'],
						"amount" =>$dealer_recharge_en['amount'],
						"begin_amount" =>$dealer_en['amount'],
						"end_amount" =>$dealer_recharge_en['amount']+$dealer_en['amount'],
						"remark" =>I('remark'),
						"status" =>1,
						'create_user' => "whaleyinfo.order DealerBiz.financeDepartmentAudit",
						'create_date' => date ( "y-m-d H:i:s", time () )
				);
				M('dealer_amount')->add($dealer_amount);
				//更新审批日志表
				$dealer_recharge_log = array (
						'change_id' => DealerBiz::generateDealerAmountChangeId (),
						'dealer_recharge_id' => $dealer_recharge_id,
						'dealer_id' => $dealer_recharge_en ['dealer_id'],
						'dealer_name' => $dealer_recharge_en ['dealer_name'],
						'recharge_status_text' => StringBiz::getDealerRechargeTypeRevenueStatusText ( 4 ),
						'recharge_status' => 4,
						'role_type' => $role_type,
						'role_id' => $role_id,
						'role_name' => $role_name,
						'remark' => I ( 'remark' ),
						'create_user' => "whaleyinfo.order VRDealerController.financeDepartmentAudit",
						'create_date' => date ( "y-m-d H:i:s", time () )
				);
				M ( 'dealer_recharge_status_change_log' )->add ( $dealer_recharge_log );
				//财务审批完毕4->5 充值完成
				M ( 'dealer_recharge' )->where ( "dealer_recharge_id='%s'", $dealer_recharge_id )->setField ( "recharge_status", 5 );
				
				//更新充值日志表
				$dealer_recharge_log1 = array (
						'change_id' => DealerBiz::generateDealerAmountChangeId (),
						'dealer_recharge_id' => $dealer_recharge_id,
						'dealer_id' => $dealer_recharge_en ['dealer_id'],
						'dealer_name' => $dealer_recharge_en ['dealer_name'],
						'recharge_status_text' => StringBiz::getDealerRechargeTypeRevenueStatusText ( 5 ),
						'recharge_status' => 5,
						'role_type' => $role_type,
						'role_id' => $role_id,
						'role_name' => $role_name,
						'remark' => I ( 'remark' ),
						'create_user' => "whaleyinfo.order VRDealerController.financeDepartmentAudit",
						'create_date' => date ( "y-m-d H:i:s", time () )
				);
				M ( 'dealer_recharge_status_change_log' )->add ( $dealer_recharge_log1 );
				
				//更新VR渠道经销商表余额字段
				M('dealer_info')->where( "dealer_id='%s'", $dealer_recharge_en['dealer_id'])->setField("amount",$dealer_en['amount']+$dealer_recharge_en['amount']);
				
				
				
				$error_msg = "财务部审批成功!";
				redirect ( C ( 'AdminConfig.info_page' ) . '?type=202&is_show_button=2&msg=' . $error_msg );
				return;
			case 4 :
				// 财务部审批失败 3->11
				$remark= I('remark');
				if(empty($remark)){
						
					$error_msg = "请输入审批人意见!";
					redirect ( C ( 'AdminConfig.error_info_page' ) . '?type=202&is_show_button=2&msg=,' . $error_msg );
					return;
				}
				M ( 'dealer_recharge' )->where ( "dealer_recharge_id='%s'", $dealer_recharge_id )->setField ( "recharge_status", 11 );
				$add_dealer_recharge_log = array (
						'change_id' => DealerBiz::generateDealerAmountChangeId (),
						'dealer_recharge_id' => $dealer_recharge_id,
						'dealer_id' => $dealer_recharge_en ['dealer_id'],
						'dealer_name' => $dealer_recharge_en ['dealer_name'],
						'recharge_status_text' => StringBiz::getDealerRechargeTypeRevenueStatusText ( 11 ),
						'recharge_status' => 11,
						'role_type' => $role_type,
						'role_id' => $role_id,
						'role_name' => $role_name,
						'remark' => I ( 'remark' ),
						'create_user' => "whaleyinfo.order VRDealerController.financeDepartmentAudit",
						'create_date' => date ( "y-m-d H:i:s", time () )
				);
				M ( 'dealer_recharge_status_change_log' )->add ( $add_dealer_recharge_log );
				$error_msg = "财务部审批已拒绝!";
				redirect ( C ( 'AdminConfig.info_page' ) . '?type=202&is_show_button=2&msg=' . $error_msg );
				return;
			default :
				$error_msg = "操作非法,请检查!";
				redirect ( C ( 'AdminConfig.error_info_page' ) . '?type=202&is_show_button=2&msg=,' . $error_msg );
				return;
		}
		}
		
	
		
	}
